﻿using System;
using System.Collections.Generic;

namespace Sail.Caching
{
    /// <summary>
    /// 提供并行的缓存上下文服务
    /// </summary>
    public interface IParallelCacheContext
    {
        /// <summary>
        ///创建一个依赖缓存上下文的任务
        /// </summary>
        ITask<T> CreateContextAwareTask<T>(Func<T> function);

        IEnumerable<TResult> RunInParallel<T, TResult>(IEnumerable<T> source, Func<T, TResult> selector);
    }

    public interface ITask<T> : IDisposable
    {
        /// <summary>
        /// 执行任务和收集令牌
        /// </summary>
        T Execute();

        /// <summary>
        /// 任务执行期间返回令牌
        /// </summary>
        IEnumerable<IVolatileToken> Tokens { get; }

        /// <summary>
        /// 回收缓存上下文令牌
        /// </summary>
        void Finish();
    }
}